﻿@using SmartStore.Core.Domain.DataExchange
@using SmartStore.Services.DataExchange.Export
@using SmartStore.Admin.Models.DataExchange
@model ExportProfileModel

@if (!Model.Provider.IsFileBasedExport)
{
	<div class="alert alert-info alert-dismissible">
		@Html.Raw(T("Admin.DataExchange.Export.NonFileBasedExport.Note"))
		<button type="button" class="close" data-dismiss="alert">&times;</button>
	</div>
}

<table class="adminContent">
	<tr>
		<td class="adminTitle">
			@Html.SmartLabelFor(x => x.FolderName)
		</td>
		<td class="adminData">
			@Html.TextBoxFor(x => x.FolderName)
			@Html.ValidationMessageFor(x => x.FolderName)
		</td>
	</tr>
	@if (Model.Provider.IsFileBasedExport)
	{
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(x => x.FileNamePattern)
			</td>
			<td class="adminData">
				<div>
					@Html.TextBoxFor(x => x.FileNamePattern)
					@Html.ValidationMessageFor(x => x.FileNamePattern)
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
			</td>
			<td class="adminData">
				<div>
					<button id="FileNamePatternExampleButton" type="button" class="btn btn-sm btn-secondary mr-3">
						<i class="fa fa-sync"></i>
						<span>@T("Admin.DataExchange.Export.Deployment.UpdateExampleFileName")</span>
					</button>
					<span id="FileNamePatternExample">@Model.FileNamePatternExample</span>
				</div>
				<div>
					<div class="mt-2">
						<button id="FileNamePatternDescriptionLink" type="button" class="btn btn-outline-light btn-sm font-weight-normal">
                            <span>@T("Admin.DataExchange.Export.Deployment.ShowPlaceholder")</span>
                            <i class="fa fa-chevron-down"></i>
						</button>
					</div>
					<div id="FileNamePatternDescription" class="hide mt-2">
						@FileNamePatternDescription()
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(x => x.CreateZipArchive)
			</td>
			<td class="adminData">
				@Html.EditorFor(x => x.CreateZipArchive)
				@Html.ValidationMessageFor(x => x.CreateZipArchive)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(x => x.EmailAccountId)
			</td>
			<td class="adminData">
				@Html.DropDownListFor(x => x.EmailAccountId, Model.AvailableEmailAccounts, Model.UnspecifiedString)
				@Html.ValidationMessageFor(x => x.EmailAccountId)
			</td>
		</tr>
		<tr id="CompletedEmailAddressesContainer">
			<td class="adminTitle">
				@Html.SmartLabelFor(x => x.CompletedEmailAddresses)
			</td>
			<td class="adminData">
				@Html.ListBoxFor(model => model.CompletedEmailAddresses, Model.AvailableCompletedEmailAddresses, new { multiple = "multiple", data_tags = "true" })
				@Html.ValidationMessageFor(x => x.CompletedEmailAddresses)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(x => x.Cleanup)
			</td>
			<td class="adminData">
				@Html.EditorFor(x => x.Cleanup)
				@Html.ValidationMessageFor(x => x.Cleanup)
			</td>
		</tr>
	}
	else
	{
		@Html.HiddenFor(model => model.FileNamePattern)
	}
</table>

@if (Model.Provider.IsFileBasedExport)
{
	<div class="admin-config-group">
		<div class="title">@T("Admin.DataExchange.Export.Deployment.ProfilesTitle")</div>
	</div>

	<div class="alert alert-info alert-dismissible">
		@Html.Raw(T("Admin.DataExchange.Export.Deployment.Note"))
		<button type="button" class="close" data-dismiss="alert">&times;</button>
	</div>

	if (!Model.Deployments.Any())
	{
		<div class="alert alert-info alert-dismissible">
			@T("Admin.DataExchange.Export.Deployment.NoProfiles")
			<button type="button" class="close" data-dismiss="alert">&times;</button>
		</div>
	}

	if (Model.Deployments.Any())
	{
		<div class="table-responsive">
			<table id="export-deployment-list" class="table admin-table profile-list">
				<thead>
					<tr>
						<th>@T("Admin.DataExchange.Export.Deployment.Name")</th>
						<th>@T("Admin.DataExchange.Export.Deployment.PublishingTarget")</th>
						<th class="text-center">@T("Common.Enabled")</th>
						<th class="text-center">@T("Admin.DataExchange.Export.ExportFiles")</th>
						<th>@T("Admin.Common.LastRun")</th>
						<th>@T("Admin.Common.Actions")</th>
					</tr>
				</thead>
				<tbody>
					@foreach (var deployment in Model.Deployments)
                    {
                        var result = deployment.LastResult;
						<tr class="@(deployment.Enabled ? "" : "disabled")" data-deployment-id="@deployment.Id">
							<td>
								<div>
									<a href="@Url.Action("EditDeployment", new { id = deployment.Id })">@deployment.Name</a>
								</div>
								<div class="muted">
									<i class="@(deployment.DeploymentType.GetIconClass())"></i>&nbsp;@deployment.DeploymentTypeName
								</div>
							</td>
							<td>
								@if (deployment.DeploymentType == ExportDeploymentType.FileSystem)
                                {
									@deployment.FileSystemPath.NaIfEmpty()
                                }
                                else if (deployment.DeploymentType == ExportDeploymentType.Email)
                                {
									@(string.Join(", ", deployment.EmailAddresses).NaIfEmpty())
                                }
                                else if (deployment.DeploymentType == ExportDeploymentType.Http || deployment.DeploymentType == ExportDeploymentType.Ftp)
                                {
									@deployment.Url.NaIfEmpty()
                                }
                                else if (deployment.DeploymentType == ExportDeploymentType.PublicFolder)
                                {
									@deployment.PublicFolderUrl.NaIfEmpty()
                                }
							</td>
							<td class="text-center">
								@Html.SymbolForBool(deployment.Enabled)
							</td>
							<td class="text-center">
								@{ Html.RenderPartial("ProfileFileCount", deployment.FileCount); }
							</td>
							<td>
								@if (result == null)
								{
									<div class="muted">@T("Common.Never")</div>
								}
								else if (result.Succeeded)
								{
									<div><span>@(T("Admin.Common.SuccessfulOn")): </span><span>@result.Execution.ToString("g")</span></div>
									<div class="muted">@result.ExecutionPretty</div>
								}
								else
								{
									<div>@result.Execution.ToString("g")</div>
									<div class="text-danger"><strong>@T("Common.Error"): </strong> <span>@result.Error</span></div>
								}
							</td>
							<td align="right" class="omega">
								<button type="button" data-url="@Url.Action("DeleteDeployment", new { id = deployment.Id })" class="btn btn-sm btn-danger delete-depoyment">
									<span>@T("Common.Delete")</span>
								</button>
							</td>
						</tr>
					}
				</tbody>
			</table>
		</div>
	}

	<a href="@Url.Action("CreateDeployment", new { id = Model.Id })" class="btn btn-warning">
		<i class="fa fa-plus"></i>
		<span>@T("Admin.DataExchange.AddNewProfile")</span>
	</a>
}

@{Html.SmartStore().Window()
	.Name("deployment-file-dialog")
	.Title(T("Admin.DataExchange.Export.ExportFiles"))
	.Size(WindowSize.Large)
	.FooterContent(@<text><button class="btn btn-secondary btn-flat" data-dismiss="modal">@T("Common.Cancel")</button></text>)
	.Render();
}

@helper FileNamePatternDescription()
{
	<div class="table-responsive">
		<table class="table admin-table">
			<thead>
				<tr>
					<th>@T("Admin.Common.Placeholder")</th>
					<th>@T("Common.Description")</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>%Profile.Id%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(0)</td>
				</tr>
				<tr>
					<td>%Profile.FolderName%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(1)</td>
				</tr>
				<tr>
					<td>%Profile.SeoName%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(2)</td>
				</tr>
				<tr>
					<td>%Store.Id%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(3)</td>
				</tr>
				<tr>
					<td>%Store.SeoName%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(4)</td>
				</tr>
				<tr>
					<td>%File.Index%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(5)</td>
				</tr>
				<tr>
					<td>%Random.Number%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(6)</td>
				</tr>
				<tr>
					<td>%Timestamp%</td>
					<td>@Model.FileNamePatternDescriptions.SafeGet(7)</td>
				</tr>
			</tbody>
		</table>
	</div>
}

<script type="text/javascript">
	$(document).ready(function () {

		// show/hide file name pattern descriptions
		$('#FileNamePatternDescriptionLink').click(function () {
            var descriptions = $('#FileNamePatternDescription'),
                isVisible = descriptions.is(':visible');

            descriptions.slideToggle();
            $(this).find('span').html(isVisible ? @T("Admin.DataExchange.Export.Deployment.ShowPlaceholder").JsText : @T("Admin.DataExchange.Export.Deployment.HidePlaceholder").JsText);
            $(this).find('i').removeClass(isVisible ? 'fa-chevron-up' : 'fa-chevron-down').addClass(isVisible ? 'fa-chevron-down' : 'fa-chevron-up');
		});

		// delete deployment
		$('#export-deployment-list').find('.delete-depoyment').click(function () {
			$(this).doPostData({
				ask: @T("Admin.Common.AskToProceed").JsText
			});
		});

		// show deployment file dialog
		$(document).on('click', '.export-file-count', function (e) {
			e.preventDefault();
			var fileDialog = $('#deployment-file-dialog'),
				id = $(this).closest('tr').attr('data-deployment-id');

			fileDialog.find('.modal-body').empty();

			$({}).doAjax({
				type: 'GET',
				url: '@Url.Action("ProfileFileDetails")?profileId=0&deploymentId=' + id,
				smallIcon: fileDialog.find('.modal-body'),
				callbackSuccess: function (response) {
					fileDialog.find('.modal-body').html(response);
				}
			});

			fileDialog.modal('show');
			return false;
		});

	});
</script>